﻿using System.Text.RegularExpressions;
using System;
using System.Windows.Browser;
using System.Net;

/// <summary>
/// String related static methods
/// </summary>
public static class StringHelper
{
    private static Regex rxURLPattern = new Regex(@"((https?|ftp)://)([\w#?=:&%\.\-~@\!\$|^\*\/\|\[\]]*)?", RegexOptions.IgnoreCase | RegexOptions.Multiline);

    public static MatchCollection GetHyperlinks(string matchText)
    {
        return rxURLPattern.Matches(matchText);
    }
    public static string StripHTML(this string text)
    {
        return HttpUtility.HtmlDecode(Regex.Replace(text, @"<(.|\n)*?>", string.Empty));
    }

    public static Boolean ContainsHyperlink(string matchText)
    {
        return rxURLPattern.IsMatch(matchText);
    }

    private static Regex rxTwit = new Regex("@([A-Za-z0-9_]*)", RegexOptions.Singleline);
    public static String GetTwit(this string text)
    {
        return rxTwit.Match(text).Groups[1].Value; ;
    }

    private static Regex rxSourceText = new Regex(@">(.*?)<", RegexOptions.Multiline);
    private static Regex rxSourceURL = new Regex("href=\"(.*?)\"", RegexOptions.Multiline);

    public static string GetSourceText(string Source)
    {
        if (string.IsNullOrEmpty(Source))
        {
            return String.Empty;
        }
        else if (rxSourceText.IsMatch(Source))
        {
            return rxSourceText.Match(Source).Groups[1].Value;
        }
        else
        {
            return Source;
        }
    }
    public static string GetSourceURL(string Source)
    {
        if (string.IsNullOrEmpty(Source))
        {
            return String.Empty;
        }
        else if (rxSourceURL.IsMatch(Source))
        {
            return rxSourceURL.Match(Source).Groups[1].Value;
        }
        else
        {
            return "http://twitter.com";
        }
    }
}
